{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4c07cdc9-bce0-49ad-85c7-14f1872b8519",
   "metadata": {},
   "source": [
    "# Python to CPP using Qwen2.5-Coder-32B-Instruct with Hyperbolic Inference Endpoint in Windows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f051c517-c4fd-4248-98aa-b808fae76cf6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import io\n",
    "import sys\n",
    "import gradio as gr\n",
    "import subprocess\n",
    "from dotenv import load_dotenv\n",
    "from huggingface_hub import InferenceClient\n",
    "from google import genai\n",
    "from google.genai import types\n",
    "from mistralai import Mistral"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6c8777b-57bc-436a-978f-21a37ea310ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load Api Keys from env\n",
    "\n",
    "load_dotenv(override=True)\n",
    "\n",
    "hf_api_key = os.getenv(\"HF_TOKEN\")\n",
    "gemini_api_key = os.getenv(\"GOOGLE_API_KEY\")\n",
    "mistral_api_key = os.getenv(\"MISTRAL_API_KEY\")\n",
    "\n",
    "if not mistral_api_key or not gemini_api_key or not hf_api_key:\n",
    "    print(\"API Key not found!\")\n",
    "else:\n",
    "    print(\"API Key loaded in memory\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5cf6f93-7e07-40e0-98b8-d4e74ea18402",
   "metadata": {},
   "outputs": [],
   "source": [
    "# MODELs \n",
    "\n",
    "MODEL_QWEN = \"Qwen/Qwen2.5-Coder-32B-Instruct\"\n",
    "MODEL_GEMINI = 'gemini-2.5-flash'\n",
    "MODEL_CODESTRAL = 'codestral-latest'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "689547c3-aaa5-4800-86a2-da52765997d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load Clients\n",
    "\n",
    "try:\n",
    "    gemini_client = genai.Client(api_key=gemini_api_key)\n",
    "    print(\"Google GenAI Client initialized successfully!\")\n",
    "\n",
    "    codestral_client = Mistral(api_key=mistral_api_key)\n",
    "    print(\"Mistral Client initialized successfully!\")\n",
    "    \n",
    "    hf_client = InferenceClient(provider=\"hyperbolic\",api_key=hf_api_key)\n",
    "    print(\"Hyperbolic Inference Client initialized successfully!\")\n",
    "except Exception as e:\n",
    "    print(f\"Error initializing Client: {e}\")\n",
    "    exit() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c3a81f4-99c3-463a-ae30-4656a7a246d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "system_message = \"You are an assistant that reimplements Python code in high-performance C++ optimized for a Windows PC. \"\n",
    "system_message += \"Use Windows-specific optimizations where applicable (e.g., multithreading with std::thread, SIMD, or WinAPI if necessary). \"\n",
    "system_message += \"Respond only with the equivalent C++ code; include comments only where absolutely necessary. \"\n",
    "system_message += \"Avoid any explanation or text outside the code. \"\n",
    "system_message += \"The C++ output must produce identical functionality with the fastest possible execution time on Windows.\"\n",
    "\n",
    "generate_content_config = types.GenerateContentConfig(system_instruction=system_message)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0fde9514-1005-4539-b01b-0372730ce67b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def user_prompt_for(python):\n",
    "    user_prompt = (\n",
    "        \"Convert the following Python code into high-performance C++ optimized for Windows. \"\n",
    "        \"Use standard C++20 or newer with Windows-compatible libraries and best practices. \"\n",
    "        \"Ensure the implementation runs as fast as possible and produces identical output. \"\n",
    "        \"Use appropriate numeric types to avoid overflow or precision loss. \"\n",
    "        \"Avoid unnecessary abstraction; prefer direct computation and memory-efficient structures. \"\n",
    "        \"Respond only with C++ code, include all required headers (like <iomanip>, <vector>, etc.), and limit comments to only what's essential.\\n\\n\"\n",
    "    )\n",
    "    user_prompt += python\n",
    "    return user_prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89c8b010-08dd-4695-a784-65162d82a24b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def user_message_gemini(python): \n",
    "    return types.Content(role=\"user\", parts=[types.Part.from_text(text=user_prompt_for(python))])  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66923158-983d-46f7-ab19-f216fb1f6a87",
   "metadata": {},
   "outputs": [],
   "source": [
    "def messages_for(python):\n",
    "    return [\n",
    "        {\"role\": \"system\", \"content\": system_message},\n",
    "        {\"role\": \"user\", \"content\": user_prompt_for(python)}\n",
    "    ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ab59a54-b28a-4d07-b04f-b568e6e25dfb",
   "metadata": {},
   "outputs": [],
   "source": [
    "def write_output(cpp):\n",
    "    code = cpp.replace(\"```cpp\", \"\").replace(\"```c++\", \"\").replace(\"```\", \"\").strip()\n",
    "   \n",
    "    if not \"#include\" in code:\n",
    "        raise ValueError(\"C++ code appears invalid: missing #include directives.\")\n",
    "\n",
    "    with open(\"qwenOptimized.cpp\", \"w\", encoding=\"utf-8\", newline=\"\\n\") as f:\n",
    "        f.write(code)  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e05ea9f0-6ade-4699-b5fa-fb8ef9f16bcb",
   "metadata": {},
   "source": [
    "### Python Codes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c515ce2c-1f8d-4484-8d34-9ffe1372dad4",
   "metadata": {},
   "outputs": [],
   "source": [
    "python_easy = \"\"\"\n",
    "import time\n",
    "\n",
    "def calculate(iterations, param1, param2):\n",
    "    result = 1.0\n",
    "    for i in range(1, iterations+1):\n",
    "        j = i * param1 - param2\n",
    "        result -= (1/j)\n",
    "        j = i * param1 + param2\n",
    "        result += (1/j)\n",
    "    return result\n",
    "\n",
    "start_time = time.time()\n",
    "result = calculate(100_000_000, 4, 1) * 4\n",
    "end_time = time.time()\n",
    "\n",
    "print(f\"Result: {result:.12f}\")\n",
    "print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83ab4080-71ae-45e6-970b-030dc462f571",
   "metadata": {},
   "outputs": [],
   "source": [
    "python_hard = \"\"\"# Be careful to support large number sizes\n",
    "\n",
    "def lcg(seed, a=1664525, c=1013904223, m=2**32):\n",
    "    value = seed\n",
    "    while True:\n",
    "        value = (a * value + c) % m\n",
    "        yield value\n",
    "        \n",
    "def max_subarray_sum(n, seed, min_val, max_val):\n",
    "    lcg_gen = lcg(seed)\n",
    "    random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)]\n",
    "    max_sum = float('-inf')\n",
    "    for i in range(n):\n",
    "        current_sum = 0\n",
    "        for j in range(i, n):\n",
    "            current_sum += random_numbers[j]\n",
    "            if current_sum > max_sum:\n",
    "                max_sum = current_sum\n",
    "    return max_sum\n",
    "\n",
    "def total_max_subarray_sum(n, initial_seed, min_val, max_val):\n",
    "    total_sum = 0\n",
    "    lcg_gen = lcg(initial_seed)\n",
    "    for _ in range(20):\n",
    "        seed = next(lcg_gen)\n",
    "        total_sum += max_subarray_sum(n, seed, min_val, max_val)\n",
    "    return total_sum\n",
    "\n",
    "# Parameters\n",
    "n = 10000         # Number of random numbers\n",
    "initial_seed = 42 # Initial seed for the LCG\n",
    "min_val = -10     # Minimum value of random numbers\n",
    "max_val = 10      # Maximum value of random numbers\n",
    "\n",
    "# Timing the function\n",
    "import time\n",
    "start_time = time.time()\n",
    "result = total_max_subarray_sum(n, initial_seed, min_val, max_val)\n",
    "end_time = time.time()\n",
    "\n",
    "print(\"Total Maximum Subarray Sum (20 runs):\", result)\n",
    "print(\"Execution Time: {:.6f} seconds\".format(end_time - start_time))\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31498c5c-ecdd-4ed7-9607-4d09af893b98",
   "metadata": {},
   "source": [
    "## Code Implementation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea4a4968-e04f-4939-8c42-32c960699354",
   "metadata": {},
   "outputs": [],
   "source": [
    "def stream_gemini(python):\n",
    "    stream = gemini_client.models.generate_content_stream(\n",
    "        model = MODEL_GEMINI,\n",
    "        config=generate_content_config,\n",
    "        contents=user_message_gemini(python)\n",
    "    )\n",
    "\n",
    "    cpp_code = \"\"\n",
    "    for chunk in stream:\n",
    "        chunk_text = chunk.text or \"\"\n",
    "        cpp_code += chunk_text\n",
    "        yield cpp_code.replace('```cpp\\n','').replace('```','')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69601eee-520f-4813-b796-aee9118e8a72",
   "metadata": {},
   "outputs": [],
   "source": [
    "def stream_codestral(python):\n",
    "    stream = codestral_client.chat.stream(\n",
    "        model = MODEL_CODESTRAL,\n",
    "        messages = messages_for(python),        \n",
    "    )\n",
    "\n",
    "    cpp_code = \"\"\n",
    "    for chunk in stream:\n",
    "        chunk_text = chunk.data.choices[0].delta.content or \"\"\n",
    "        cpp_code += chunk_text\n",
    "        yield cpp_code.replace('```cpp\\n','').replace('```','')    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb8899cf-54c0-4d2d-8772-42925c2e1d13",
   "metadata": {},
   "outputs": [],
   "source": [
    "def stream_qwen(python):\n",
    "    stream = hf_client.chat.completions.create(\n",
    "        model = MODEL_QWEN,\n",
    "        messages = messages_for(python),\n",
    "        stream=True\n",
    "    )\n",
    "    cpp_code = \"\"\n",
    "    for chunk in stream:\n",
    "        chunk_text = chunk.choices[0].delta.content\n",
    "        cpp_code += chunk_text\n",
    "        yield cpp_code.replace('```cpp\\n','').replace('```','')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98862fef-905c-4b50-bc7a-4c0462495b5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def optimize(python, model):\n",
    "    if model.lower() == 'gemini':\n",
    "        result = stream_gemini(python)\n",
    "    elif model.lower() == 'codestral':\n",
    "        result = stream_codestral(python)\n",
    "    elif model.lower() == 'qwen_coder':\n",
    "        result = stream_qwen(python)\n",
    "    else:\n",
    "        raise ValueError(\"Unknown model\")\n",
    "        \n",
    "    for stream_so_far in result:\n",
    "        yield stream_so_far  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa9372df-db01-41d0-842c-4857b20f93f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "custom_css = \"\"\"\n",
    ".scrollable-box textarea {\n",
    "    overflow: auto !important;\n",
    "    height: 400px;\n",
    "}\n",
    "\n",
    ".python {background-color: #306998;}\n",
    ".cpp {background-color: #050;}\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "theme = gr.themes.Soft()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dbcf9fe9-c3da-466b-8478-83dcdbe7d48e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def execute_python(code):\n",
    "    try:\n",
    "        result = subprocess.run(\n",
    "            [\"python\", \"-c\", code],\n",
    "            capture_output=True,\n",
    "            text=True,\n",
    "            timeout=60\n",
    "        )\n",
    "        if result.returncode == 0:\n",
    "            return result.stdout or \"[No output]\"\n",
    "        else:\n",
    "            return f\"[Error]\\n{result.stderr}\"\n",
    "    except subprocess.TimeoutExpired:\n",
    "        return \"[Error] Execution timed out.\"\n",
    "    except Exception as e:\n",
    "        return f\"[Exception] {str(e)}\"  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8029e00d-1ee8-43d1-8c87-2aa0544cf94c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def execute_cpp(code):\n",
    "    write_output(code)\n",
    "    \n",
    "    try:\n",
    "        compile_cmd = [\"g++\", \"-O3\", \"-std=c++20\", \"-o\", \"optimized.exe\", \"optimized.cpp\"]\n",
    "        compile_result = subprocess.run(compile_cmd, capture_output=True, text=True, check=True)\n",
    "        \n",
    "        run_cmd = [\"optimized.exe\"]\n",
    "        run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True, timeout=60)\n",
    "        \n",
    "        return run_result.stdout or \"[No output]\"\n",
    "        \n",
    "    except subprocess.CalledProcessError as e:\n",
    "        return f\"[Compile/Runtime Error]\\n{e.stderr}\"\n",
    "    except subprocess.TimeoutExpired:\n",
    "        return \"[Error] Execution timed out.\"\n",
    "    except Exception as e:\n",
    "        return f\"[Exception] {str(e)}\"  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5f4e88c-be15-4870-9f99-82b6273ee739",
   "metadata": {},
   "outputs": [],
   "source": [
    "with gr.Blocks(css=custom_css, theme=theme) as ui:\n",
    "    gr.Markdown(\"## Convert code from Python to C++\")\n",
    "    with gr.Row():\n",
    "        python = gr.Textbox(label=\"Python code:\", lines=10, value=python_hard, elem_classes=[\"scrollable-box\"])\n",
    "        cpp = gr.Textbox(label=\"C++ code:\", lines=10, elem_classes=[\"scrollable-box\"])\n",
    "    with gr.Row():\n",
    "        model = gr.Dropdown([\"Gemini\", \"Codestral\", \"QWEN_Coder\"], label=\"Select model\", value=\"Gemini\")\n",
    "        convert = gr.Button(\"Convert code\")\n",
    "    with gr.Row():\n",
    "        python_run = gr.Button(\"Run Python\")\n",
    "        cpp_run = gr.Button(\"Run C++\")\n",
    "    with gr.Row():\n",
    "        python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
    "        cpp_out = gr.TextArea(label=\"C++ result:\", elem_classes=[\"cpp\"])\n",
    "\n",
    "    convert.click(optimize, inputs=[python,model], outputs=[cpp])\n",
    "    python_run.click(execute_python,inputs=[python], outputs=[python_out])\n",
    "    cpp_run.click(execute_cpp, inputs=[cpp], outputs=[cpp_out])\n",
    "\n",
    "ui.launch(inbrowser=True)   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa1a231e-2743-4cee-afe2-783d2b9513e5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
